package com.onlinevideo.dao.impl;

import com.onlinevideo.dao.AuthorityDao;
import com.onlinevideo.dao.BaseDao;
import com.onlinevideo.entity.Authority;


import java.util.List;

public class AuthorityDaoImpl extends BaseDao implements AuthorityDao {


    @Override
    public int insert(Authority authority) {
        String sql ="INSERT INTO `T_authority` (`url`,`authority_description`) VALUES (?,?)";
        return super.update(sql,authority.getUrl(),authority.getAuthorityDescription());

    }

    @Override
    public int delete(Long id) {
        String sql = "DELETE FROM `T_authority` WHERE `id`=?";
        return super.update(sql,id);
    }

    @Override
    public int update(Authority authority) {
        String sql = "UPDATE `T_authority` SET `url`=?,`authority_description`=? WHERE `id`=?";
        return super.update(sql,authority.getUrl(),authority.getAuthorityDescription(),authority.getId());
    }

    @Override
    public Authority queryById(Long id) {
        String sql =  "SELECT `id`,`url`,`authority_description` AS `authorityDescription` FROM `T_authority` WHERE 1=1 AND `id`=?";
        return super.queryForOne(Authority.class,sql,id);

    }

    @Override
    public List<Authority> queryAll() {
        String sql =  "SELECT `id`,`url`,`authority_description` AS `authorityDescription` FROM `T_authority` WHERE 1=1";
        return super.queryForList(Authority.class,sql);
    }

    @Override
    public List<Authority> queryByRoleId(Long roleId) {
        String sql =  "SELECT A.`id`,`url`,`authority_description` AS `authorityDescription` FROM `T_authority` AS A, `t_role_authority` AS R WHERE 1=1 AND  A.`id` = `authority_id` AND `role_id` = ?  ";
        return super.queryForList(Authority.class,sql,roleId);
    }
}
